package com.example.mymicaihu.adapter.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.adapter.BaseAdapter;
import com.example.mvplibrary.adapter.BaseViewHolder;
import com.example.mymicaihu.R;
import com.example.mymicaihu.bean.VideoBean;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;


/**
 * 作者：朱勇闯
 * data : 2021/4/25  17:02
 * 作用： xxxx
 */
public class VideoAdapter extends BaseAdapter<VideoBean.NewsListDTO> {
    private static final String TAG = "朱勇闯videoAdapter";
    public ItemListener itemListener;

    public void setItemListener(ItemListener itemListener) {
        this.itemListener = itemListener;
    }

    public VideoAdapter(List<VideoBean.NewsListDTO> datas, Context context, int layoutId) {
        super(datas, context, layoutId);
    }

    @Override
    protected void bindData(BaseViewHolder holder, VideoBean.NewsListDTO listBean, int position) {
        TextView name = holder.getView(R.id.tv_fragment_video_name);
        TextView title = holder.getView(R.id.tv_fragment_video_title);
        TextView desc = holder.getView(R.id.tv_fragment_video_desc);
        TextView praise = holder.getView(R.id.tv_fragment_video_praise);
        TextView comments = holder.getView(R.id.tv_fragment_video_comments);
        ImageView head = holder.getView(R.id.iv_fragment_video_head);
        StandardGSYVideoPlayer video = holder.getView(R.id.video_fragment_video);
        name.setText(listBean.getAuthorName());
        praise.setText(listBean.getPraiseNum());
        comments.setText(listBean.getCommentNum());
        title.setText(listBean.getArticleTitle());
        desc.setText(listBean.getClickNum());

        video.setUp(listBean.getVideoUrl(), true, listBean.getArticleTitle());

        Glide.with(context).load(listBean.getAuthorHeadPic()).into(head);
    }


    public interface ItemListener {
        void onItemClickListener(int pos);
    }


    public static Bitmap createVideoThumbnail(String filePath) {
        // MediaMetadataRetriever is available on API Level 8
        // but is hidden until API Level 10
        Class<?> clazz = null;
        Object instance = null;
        try {
            clazz = Class.forName("android.media.MediaMetadataRetriever");
            instance = clazz.newInstance();

            Method method = clazz.getMethod("setDataSource", String.class);
            method.invoke(instance, filePath);

            // The method name changes between API Level 9 and 10.
            if (Build.VERSION.SDK_INT <= 9) {
                return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
            } else {
                byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
                if (data != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    if (bitmap != null) return bitmap;
                }
                return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
            }
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } catch (InstantiationException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (InvocationTargetException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } finally {
            try {
                if (instance != null) {
                    clazz.getMethod("release").invoke(instance);
                }
            } catch (Exception ignored) {
            }
        }
        return null;
    }
}
